<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, FormInstance, FormRules, genFileId, UploadFile, UploadFiles, UploadInstance, UploadProps, UploadRawFile } from 'element-plus';
import useStore from '@/store';
import { props } from './receiveorder';
defineOptions({ name: 'ReceiveOrderForm' });
const _props = defineProps(props);
const save = async () => {
  if (!ruleFormRef.value) return;
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      if (form.xyTable == null) {
        ElMessage.error('请导入XY表!');
        return;
      }
      window.$api.actionLog.save(form).then(res => {
        ElMessage.success('收货成功!');
        useStore.window.windowRemove(_props.window?.name);
        if (_props.window?.callBack) _props.window?.callBack();
      });
    }
  });
};
const tableRef = ref(null);
const serialNumberTypes = ref(null);
const orders = ref(null);
const disabled = ref(false);
const loading = ref(false);
const loadingDownLoad = ref(false);
const ruleFormRef = ref<FormInstance>();
const form = reactive({
  id: null,
  number: null,
  actionLogNumber: null,
  createdOn: null,
  warehouseId: null,
  lensId: null,
  warehouseCode: null,
  warehouseName: null,
  lensCode: null,
  lensName: null,
  relationType: null,
  remark: null,
  xyTable: null,
});
const rules = reactive<FormRules>({
  relationType: [{ required: true, message: '请选择订单类型', trigger: 'change' }],
  id: [{ required: true, message: '请选择订单号', trigger: 'change' }],
});
/** 加载基础数据,字典等 */
const init = async () => {
  await window.$api.common.getSerialNumberTypes().then(res => {
    serialNumberTypes.value = res.data;
  });
  if (_props.window.params) {
    if (_props.window.params.type) {
      form.relationType = _props.window.params.type;
    }
    if (_props.window.params.id) {
      await getCanReceiveOrder();
      form.id = _props.window.params.id;
      idChange(form.id);
      disabled.value = true;
    }
  }
};
const relationTypeChange = async val => {
  form.relationType = val;
  form.id = null;
  form.number = null;
  form.actionLogNumber = null;
  form.createdOn = null;
  form.warehouseId = null;
  form.lensId = null;
  form.warehouseCode = null;
  form.warehouseName = null;
  form.lensCode = null;
  form.lensName = null;
  await getCanReceiveOrder();
};
const idChange = val => {
  let order = orders.value.find(item => item.id == val);
  form.number = order.number;
  form.actionLogNumber = order.actionLogNumber;
  form.createdOn = order.createdOn;
  form.warehouseId = order.warehouseId;
  form.lensId = order.lensId;
  form.warehouseCode = order.warehouseCode;
  form.warehouseName = order.warehouseName;
  form.lensCode = order.lensCode;
  form.lensName = order.lensName;
};

const getCanReceiveOrder = async () => {
  await window.$api.actionLog.getCanReceiveOrder(form.relationType).then(res => {
    orders.value = res.data;
  });
};
const showXYTable = () => {
  let params = new FormData();
  params.append('file', form.file);
  loading.value = true;
  window.$api.common.getXYTable(params).then(res => {
    form.xyTable = res.data;
    loading.value = false;
  });
};

const upload = ref<UploadInstance>();

const handleExceed: UploadProps['onExceed'] = files => {
  upload.value!.clearFiles();
  const file = files[0] as UploadRawFile;
  file.uid = genFileId();
  upload.value!.handleStart(file);
};
const handleFileChange = (uploadFile: UploadFile, uploadFiles: UploadFiles) => {
  form.file = uploadFile.raw;
};
const exportLackReceive = () => {
  if (form.id == null) {
    ElMessage.error('请选择订单号!');
    return;
  }
  loadingDownLoad.value = true;
  let params = { id: form.id, relationType: form.relationType };
  window.$api.actionLog.exportLackReceive(params).then(res => {
    loadingDownLoad.value = false;
  });
};
const exportCurrentStock = () => {
  if (form.id == null) {
    ElMessage.error('请选择订单号!');
    return;
  }
  loadingDownLoad.value = true;
  let params = { warehouseId: form.warehouseId, lensId: form.lensId };
  window.$api.stock.exportCurrentStock(params).then(res => {
    loadingDownLoad.value = false;
  });
};
onMounted(() => {
  init();
});
</script>

<template>
  <windows-window :window="window" v-loading="loadingDownLoad">
    <template #button>
      <windows-button-group size="18">
        <windows-button icon="fluent-save-28-regular" split @click="save"></windows-button>
        <windows-button icon="fluent-contract-down-left-28-regular" @click="exportLackReceive">导出缺少收货</windows-button>
        <windows-button icon="fluent-contract-down-left-28-regular" @click="exportCurrentStock">导出现时库存</windows-button>
      </windows-button-group>
    </template>
    <el-form ref="ruleFormRef" :model="form" :rules="rules" label-width="80px" class="demo-ruleForm" size="default" status-icon>
      <el-row>
        <el-col :span="12">
          <el-form-item label="订单类型" prop="relationType">
            <el-select v-model="form.relationType" @change="relationTypeChange" :disabled="disabled">
              <el-option v-for="item in serialNumberTypes" :key="item.value" :label="item.name" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="订单号" prop="id">
            <el-select v-model="form.id" @change="idChange" :disabled="disabled">
              <el-option v-for="item in orders" :key="item.id" :label="item.number" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="收货单号" prop="actionLogNumber">
            <el-input disabled v-model="form.actionLogNumber" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="订单日期" prop="createdOn">
            <el-input disabled v-model="form.createdOn" placeholder="" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="仓库代码" prop="warehouseCode">
            <el-input disabled v-model="form.warehouseCode" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="仓库名称" prop="warehouseName">
            <el-input disabled v-model="form.warehouseName" placeholder="" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="镜种代码" prop="lensCode">
            <el-input disabled v-model="form.lensCode" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="镜种名称" prop="lensName">
            <el-input disabled v-model="form.lensName" placeholder="" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="选择文件">
            <el-upload ref="upload" style="width: 100%" drag :limit="1" :auto-upload="false" :on-exceed="handleExceed" :on-change="handleFileChange">
              <el-icon class="el-icon--upload"><upload-filled /></el-icon>
              <div class="el-upload__text">拖动文件到此或者点击选择文件</div>
            </el-upload>
            <el-button type="success" icon="UploadFilled" @click="showXYTable">导入</el-button>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-table ref="tableRef" :data="form.xyTable" style="width: 100%" height="400" highlight-current-row v-loading="loading" border>
            <el-table-column prop="sph" label="SPH" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="000" label="000" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-025" label="-025" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-050" label="-050" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-075" label="-075" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-100" label="-100" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-125" label="-125" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-150" label="-150" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-175" label="-175" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-200" label="-200" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-225" label="-225" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-250" label="-250" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-275" label="-275" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-300" label="-300" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-325" label="-325" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-350" label="-350" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-375" label="-375" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-400" label="-400" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-425" label="-425" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-450" label="-450" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-475" label="-475" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-500" label="-500" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-525" label="-525" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-550" label="-550" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-575" label="-575" align="center" minWidth="80" show-overflow-tooltip></el-table-column>
            <el-table-column prop="-600" label="-600" align="center" minWidth="60" show-overflow-tooltip></el-table-column>
            <el-table-column prop="total" label="Total" align="center" minWidth="60" show-overflow-tooltip fixed="right"></el-table-column>
          </el-table>
        </el-col>
      </el-row>
    </el-form>
  </windows-window>
</template>

<style scoped lang="scss">
#edit {
  height: 100%;
}
.el-form {
  margin: 30px 20px;
  .el-upload-dragger {
    padding: 0;
  }
}
.custom-node {
  display: flex;
  align-items: center;
  span {
    margin-left: 5px;
  }
}
.select-icon {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
  margin-top: 5px;
  background: #fff;
  width: 100%;
  height: 200px;
  overflow: auto;
  .el-image {
    margin: 10px;
    cursor: pointer;
    border-radius: 5px;
    padding: 5px;
    &:hover,
    &.active {
      background: #dcdfe6;
    }
  }
}
</style>
